<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">

      <el-form-item label="批次号" prop="batchId">
        <el-input
          v-model="queryParams.batchId"
          placeholder="请输入批次号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传日期" prop="uploadDate">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="verifyStatus">
        <el-select v-model="form.regChannel" placeholder="请选择审核状态">
          <el-option
            v-for="dict in verifyStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核日期" prop="verifyDate">
        <el-date-picker
          v-model="dateRange2"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-col :span="1.5">
      <el-button
        type="primary"
        size="mini"
      >
        <el-link style="color:white;font-size: 12px;" icon="el-icon-download" :underline="false"
                 href="http://storage.guzift.com/template/%E7%94%A8%E6%88%B7%E7%AD%BE%E7%BA%A6%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E6%A0%BC%E5%BC%8F%E6%A8%A1%E6%9D%BF.xlsx">
          下载导入模板
        </el-link>
      </el-button>
    </el-col>


    <el-col :span="1.5">
      <el-upload
        class="upload-demo"
        :action="excelUploadUrl"
        :headers="myHeaders"
        :show-file-list="false"
        :before-upload="beforeAvatarUpload"
        :on-success="handleImageBatchUpload"
        :multiple="false">
        <el-tooltip placement="top">
          <div slot="content">
            1、上传文件必须按照模板形式上传<br/>
            2、上传文件只能是 xls、xlsx格式<br/>
            3、上传文件大小不能超过1M<br/>
          </div>
          <el-button
            type="success"
            icon="el-icon-upload"
            size="mini"
            v-hasPermi="['fund:payFile:upload']"
          >文件上传
          </el-button>
        </el-tooltip>

      </el-upload>
    </el-col>

    <el-table v-loading="loading" :data="payFileList" @selection-change="handleSelectionChange">
      <!--el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" align="center" prop="id"/-->
      <el-table-column label="商户编号" align="center" prop="merId"/>
      <el-table-column label="下发渠道" align="center" prop="payChannel" :formatter="payChannelFormat"/>
      <el-table-column label="批次号" align="center" 　 prop="batchId" width="300"/>
      <el-table-column label="文件名称" align="center" prop="fileName" width="180"/>
      <el-table-column label="下发总数量" align="center" prop="fileCount"/>
      <el-table-column label="下发总金额" align="center" prop="fileAmount"/>
      <el-table-column label="上传日期" align="center" prop="uploadDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.uploadDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="verifyStatus">
        <template slot-scope="scope">
      <span v-if="scope.row.verifyStatus == 2">
          <el-tag type="success" effect="dark" size="mini">审核通过</el-tag>
      </span>
          <span v-if="scope.row.verifyStatus == 3">
          <el-tag type="danger" effect="dark" size="mini">审核拒绝</el-tag>
      </span>
          <span v-if="scope.row.verifyStatus == 1">
          <el-tag type="info" effect="dark" size="mini">未审核</el-tag>
      </span>
        </template>
      </el-table-column>
      <el-table-column label="审核日期" align="center" prop="verifyDate" width="180">
        <template slot-scope="scope">
                     <span v-if="scope.row.verifyDate == null">
                         无
                      </span>
          <span v-if="scope.row.verifyDate != null">

                      <span>{{ parseTime(scope.row.verifyDate) }}</span>

                     </span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态 " align="center" prop="transStatus">
        <template slot-scope="scope">
      <span v-if="scope.row.transStatus == 0">
          <el-tag type="info" effect="dark" size="mini">未处理</el-tag>
      </span>
          <span v-if="scope.row.transStatus == 1">
          <el-tag type="success" effect="dark" size="mini">成功</el-tag>
      </span>
          <span v-if="scope.row.transStatus == 2">
          <el-tag type="danger" effect="dark" size="mini">失败</el-tag>
      </span>
          <span v-if="scope.row.transStatus == 3">
          <el-tag type="warning" effect="dark" size="mini">处理中</el-tag>
      </span>
        </template>
      </el-table-column>

      <el-table-column label="处理日期" align="center" prop="transDate" width="180">
        <template slot-scope="scope">
                     <span v-if="scope.row.transDate == null">
                         无
                      </span>
          <span v-if="scope.row.transDate != null">

                      <span>{{ parseTime(scope.row.transDate) }}</span>

                     </span>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row,'view')"
            v-hasPermi="['fund:payFile:edit']"
          >查看
          </el-button>
          <span v-if="scope.row.verifyStatus == 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="verifyUpdate(scope.row,'success')"
            v-hasPermi="['fund:payFile:auth']"
          >审核通过
          </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="verifyUpdate(scope.row,'fail')"
              v-hasPermi="['fund:payFile:auth']"
            >审核拒绝
          </el-button>
               </span>
          <span v-if="scope.row.verifyStatus == 2">
               <span v-if="scope.row.transStatus == 0">
                      <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-success"
                        @click="submitTransfer(scope.row)"
                        v-hasPermi="['fund:payFile:transfer']"
                      >下发
             </el-button>
                 </span>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改文件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户编号" prop="merId">
          <span>{{ (form.merId) }}</span>
        </el-form-item>
        <el-form-item label="下发渠道" prop="payChannel">
            <span v-if="form.payChannel == 1">
               <span>农行</span>
          </span>
        </el-form-item>
        <el-form-item label="批次号" prop="batchId">
          <span>{{ (form.batchId) }}</span>
        </el-form-item>
        <el-form-item label="文件名称" prop="fileName">
          <span>{{ (form.fileName) }}</span>
        </el-form-item>
        <el-form-item label="总数量" prop="fileCount">
          <span>{{ (form.fileCount) }} 笔</span>
        </el-form-item>
        <el-form-item label="总金额" prop="fileAmount">
          <span>{{ (form.fileAmount) }} 元</span>
        </el-form-item>
        <el-form-item label="上传日期" prop="uploadDate">
          <span>{{ parseTime(form.uploadDate) }}</span>
        </el-form-item>
        <el-form-item label="审核状态" prop="verifyStatus">
          <span v-if="form.verifyStatus == 1">
              <span> <el-tag type="info" effect="dark" size="mini">未审核</el-tag> </span>
          </span>
          <span v-if="form.verifyStatus == 2">
            <span> <el-tag type="success" effect="dark" size="mini">审核通过</el-tag> </span>
          </span>
          <span v-if="form.verifyStatus == 3">
             <span> <el-tag type="danger" effect="dark" size="mini">审核拒绝</el-tag> </span>
          </span>
        </el-form-item>
        <el-form-item label="审核日期" prop="verifyDate">
          <span v-if="form.verifyDate == null">
            无
          </span>
          <span v-if="form.verifyDate != null">
           {{ parseTime(form.verifyDate) }}
          </span>
        </el-form-item>
        <el-form-item label="审核描述" prop="verifyDesc">
          <span>{{ (form.verifyDesc) }}</span>
        </el-form-item>
        <el-form-item label="处理日期" prop="transDate">
         <span v-if="form.transDate == null">
            无
          </span>
          <span v-if="form.transDate != null">
           {{ parseTime(form.transDate) }}
          </span>
        </el-form-item>
        <!--el-form-item label="租户代码" prop="corpCode">
          <el-input v-model="form.corpCode" placeholder="请输入租户代码"/>
        </el-form-item>
        <el-form-item label="租户名称" prop="corpName">
          <el-input v-model="form.corpName" placeholder="请输入租户名称"/>
        </el-form-item>
        <el-form-item label="请求IP" prop="reqIp">
          <el-input v-model="form.reqIp" placeholder="请输入请求IP"/>
        </el-form-item>

        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容"/>
        </el-form-item-->
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="title !== '审核信息详情'">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import {
    listPayFile,
    getPayFile,
    delPayFile,
    addPayFile,
    updatePayFile,
    exportPayFile,
    verifyUpdateSuccessStatus,
    verifyUpdateFailStatus,
    transferSend
  } from "@/api/fund/payFile";
  import {getToken} from '@/utils/auth';

  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 文件表格数据
        payFileList: [],
        dateRange: [],
        dateRange2: [],
        //审核状态
        verifyStatusOptions: [],
        //处理状态
        transStatusOptions: [],
        //下方渠道
        payChannelOptions: [],
        excelUploadUrl: process.env.VUE_APP_BASE_API + '/fund/payFile/upload',
        myHeaders: {Authorization: 'Bearer ' + getToken()},
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          merId: undefined,
          payChannel: undefined,
          batchId: undefined,
          fileName: undefined,
          fileCount: undefined,
          fileAmount: undefined,
          uploadDate: undefined,
          verifyStatus: undefined,
          verifyDate: undefined,
          verifyDesc: undefined,
          transStatus: undefined,
          transDate: undefined,
          corpCode: undefined,
          corpName: undefined,
          reqIp: undefined,
          status: undefined,
          remarks: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          merId: [
            {required: true, message: "商户编号不能为空", trigger: "blur"}
          ], payChannel: [
            {required: true, message: "下发渠道不能为空", trigger: "blur"}
          ], batchId: [
            {required: true, message: "批次号不能为空", trigger: "blur"}
          ], fileName: [
            {required: true, message: "文件名称不能为空", trigger: "blur"}
          ], fileCount: [
            {required: true, message: "下发总数量不能为空", trigger: "blur"}
          ], fileAmount: [
            {required: true, message: "下发总金额不能为空", trigger: "blur"}
          ], uploadDate: [
            {required: true, message: "上传日期不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("verify_status").then(response => {
        this.verifyStatusOptions = response.data;
      });
      this.getDicts("trans_status").then(response => {
        this.transStatusOptions = response.data;
      });
      this.getDicts("pay_Channel").then(response => {
        this.payChannelOptions = response.data;
      });
    },
    methods: {
      // 下方渠道字段
      payChannelFormat(row, column) {
        return this.selectDictLabel(this.payChannelOptions, row.payChannel);
      },
      // 状态审核字典翻译
      verifyStatusFormat(row, column) {
        return this.selectDictLabel(this.verifyStatusOptions, row.verifyStatus);
      },
      //处理状态字典翻译
      transStatusFormat(row, column) {
        return this.selectDictLabel(this.transStatusOptions, row.transStatus);
      },
      /** 查询文件列表 */
      getList() {
        this.loading = true;
        listPayFile(this.addDateCheckRange(this.queryParams, this.dateRange, this.dateRange2)).then(response => {
          this.payFileList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          merId: undefined,
          payChannel: undefined,
          batchId: undefined,
          fileName: undefined,
          fileCount: undefined,
          fileAmount: undefined,
          uploadDate: undefined,
          verifyStatus: "0",
          verifyDate: undefined,
          verifyDesc: undefined,
          transStatus: "0",
          transDate: undefined,
          corpCode: undefined,
          corpName: undefined,
          createBy: undefined,
          createDate: undefined,
          reqIp: undefined,
          status: "0",
          updateBy: undefined,
          updateDate: undefined,
          remarks: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
          this.dateRange = [],
          this.dateRange2 = [],
          this.resetForm("queryForm");
          this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加文件";
      },
      /** 修改按钮操作 */
      handleUpdate(row, type) {
        this.reset();
        const id = row.id || this.ids
        getPayFile(id).then(response => {
          this.form = response.data;
          this.open = true;

          if ('verify' === type) {
            this.title = "修改审核信息";
          }
          if ('view' === type) {
            this.title = "审核信息详情";
          }
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updatePayFile(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addPayFile(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除文件编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delPayFile(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      //审核处理
      verifyUpdate(row, type) {
        const ids = row.id || this.ids;
        const batchs = row.batchId
        this.$confirm('是否审核批次"' + batchs + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          if ('success' === type) {
            return verifyUpdateSuccessStatus(ids);
          }
          if ('fail' === type) {
            return verifyUpdateFailStatus(ids);
          }
        }).then(() => {
          this.getList();
          this.msgSuccess("审核完成");
        }).catch(function () {
        });
      },

      //下发处理
      submitTransfer(row) {
        const ids = row.id || this.ids;
        const batchs = row.batchId
        this.$confirm('是否下发处理"' + batchs + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return transferSend(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("下发处理完成");
        }).catch(function () {
        });
      },
      beforeAvatarUpload(file) {

        var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
        const extension = testmsg === 'xls'
        const extension2 = testmsg === 'xlsx'
        const isLt2M = file.size / 1024 / 1024 < 1
        if (!extension && !extension2) {
          this.$message({
            message: '上传文件只能是 xls、xlsx格式!',
            type: 'warning'
          });
        }
        if (!isLt2M) {
          this.$message({
            message: '上传文件大小不能超过 5M!',
            type: 'warning'
          });
        }
        return extension || extension2 && isLt2M
      },
      /** 处理图片批量上传*/
      handleImageBatchUpload(res) {
        if (res.code === 200) {
          this.getList();
          this.msgSuccess("上传成功!");
        } else {
          this.msgError(res.msg);
        }
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出文件数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportPayFile(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>
